package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pojo.Cart;
import util.DataConn;

public class CartDao {
	public Cart getCartByUIDandPID(Cart cart) {
		Cart c=null;
		Connection conn = DataConn.getConn();
		String sql="select * from cart where p_id=?and c_name=?";
		
			try {
				PreparedStatement pst=conn.prepareStatement(sql);
				pst = conn.prepareStatement(sql);
				pst.setString(1, cart.getP_id());
				pst.setString(2, cart.getC_name());
				ResultSet rs = pst.executeQuery();
				if(rs.next()) {
					c=new Cart();
					c.setC_id(rs.getInt("c_id"));
					c.setC_name(rs.getString("c_name"));
					c.setP_id(rs.getString("p_id"));
					c.setQuantity(rs.getInt("quantity"));
					c.setP_price(rs.getFloat("p_price"));
					c.setAmount(rs.getFloat("amount"));
					c.setP_image(rs.getString("p_image"));
					c.setP_name(rs.getString("p_name"));
					
				}
			  
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}return c;
			
	}
	//根据用户名和商品编号更新数量
	public int updateNumByUIDandPID(Cart cart) {
		int flag=0;
		Connection conn = DataConn.getConn();
		String sql="update cart set  quantity=? where p_id=? and c_name=?";
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setInt(1, cart.getQuantity()+1);
			pst.setString(2, cart.getP_id());
			pst.setString(3, cart.getC_name());
			flag=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	//无此商品则添加
	public int insertCart(Cart c) {
		int flag=0;
		Connection conn = DataConn.getConn();
		String sql="insert into cart(c_name,p_id,quantity,p_price,amount,p_image,p_name)"
				+ " value(?,?,?,?,?,?,?)";
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setString(1, c.getC_name());
			pst.setString(2, c.getP_id());
			pst.setInt(3, c.getQuantity());
			pst.setFloat(4, c.getP_price());
			pst.setFloat(5, c.getP_price()*c.getQuantity());
			pst.setString(6, c.getP_image());
			pst.setString(7, c.getP_name());
			flag=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return flag;
	}
	public List<Cart> showCarts(String c_name){ //条件是用户账号
		List<Cart> list = new ArrayList<Cart>();
		Connection conn = DataConn.getConn();
		String sql = "select * from cart where c_name=?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1,c_name);
			ResultSet rs = pst.executeQuery();
			Cart c=null;
			while(rs.next()) {
				c = new Cart();
				c.setC_id(rs.getInt("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setP_id(rs.getString("p_id"));
				c.setQuantity(rs.getInt("quantity"));
				c.setP_price(rs.getFloat("p_price"));
				c.setAmount(rs.getFloat("amount"));
				c.setP_image(rs.getString("p_image"));
				c.setP_name(rs.getString("p_name"));
				
				list.add(c);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	//根据c_id设置数量
	public int updateNumByCID(int cid,int num) {

		int flag=0;
		Connection conn = DataConn.getConn();
		String sql="update cart set  quantity=? where c_id=?";
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setInt(1, num);
			pst.setInt(2, cid);
			
			flag=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	//删除操作
	public int delCart(String c_name,String p_id) {
		int flag=0;
		Connection conn = DataConn.getConn();
		String sql="delete from cart where c_name=? and p_id =?";
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setString(1,c_name);
			pst.setString(2, p_id);
			flag=pst.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	
	
	
	}
